package com.luck.pf.security.param.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.luck.org.entity.SysParams;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.security.param.logic.IParamLogic;
import com.luck.pf.security.param.qry.ParamQry;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

public class ParamAction extends BaseActionSupport {
	private static final long serialVersionUID = -7465315318893700798L;
	IParamLogic paramLogic;
	SysParams entity;
	Page page;
	List<SysParams> topParams;
	ParamQry qry;

	public String list() throws Exception {
		topParams = paramLogic.findTopParams();
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (ParamQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new ParamQry();
		}
		qry.setUserInfo(userBean);
		if(StringUtils.isNotBlank(qry.getOperateType()))
			page = paramLogic.findParamPage(qry);
		super.title = "参数列表";
		return "list";
	}

	public String toAdd() throws Exception{
		topParams = paramLogic.findTopParams();
		entity = new SysParams();
		entity.setId(ADD_PK);
		super.title="增加参数";
		return "edit";
	}
	public String toEdit() throws Exception{
		topParams = paramLogic.findTopParams();
		entity = paramLogic.get(SysParams.class, entity.getId());
		super.title="修改参数";
		return "edit";
	}
	public String edit() throws Exception{
		UserBean userBean = super.getUserInfo();
		Long paramId = super.getLongParameter("parentId", true);
		if(paramId != null)
			entity.setParentParam(paramLogic.get(SysParams.class, paramId));
		entity.setOperTime(DateUtil.getDate());
		entity.setOperUser(userBean.getUserId());
		entity.setParamStatus("1");
		entity.setParamType("1");
		if(entity.getId() == ADD_PK){//新增
			entity.setId(null);
			paramLogic.saveParam(entity,userBean);
		}else{//修改
			paramLogic.saveParam(entity,userBean);			
		}
		super.renderJsonSuccess("保存成功");
		return null;
	}
	public String delete() throws Exception{
		UserBean userBean = super.getUserInfo();
		entity = paramLogic.get(SysParams.class, entity.getId());
		String str = "";
		try {
			str = paramLogic.deleteParam(entity, userBean, createLog(userBean, "删除参数["+entity.getParamName()+"]", "2"));
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isNotBlank(str))
			super.renderJsonError(str+"不能删除");
		else
			super.renderJsonSuccess(str);
		return null;
	}

	public IParamLogic getParamLogic() {
		return paramLogic;
	}

	public void setParamLogic(IParamLogic paramLogic) {
		this.paramLogic = paramLogic;
	}

	public SysParams getEntity() {
		return entity;
	}

	public void setEntity(SysParams entity) {
		this.entity = entity;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<SysParams> getTopParams() {
		return topParams;
	}

	public void setTopParams(List<SysParams> topParams) {
		this.topParams = topParams;
	}

	public ParamQry getQry() {
		return qry;
	}

	public void setQry(ParamQry qry) {
		this.qry = qry;
	}
}
